package utils;

import java.io.File;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;











import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import play.libs.Codec;
import play.libs.Files;
import redis.clients.jedis.Jedis;
import services.CaseService;
import services.CourtLiveService;
import services.DrMicroService;
import models.DrCase;
import models.DrLiveTemp;
import models.DrMicrophone;

/**
 * 同步数据线程
 * @author wangshaoming
 *
 */
public class SysnDataThreadbak implements Runnable{
	public static final Logger log=LoggerFactory.getLogger("thread");
	private String caseid;
	private String courtid;
	public SysnDataThreadbak(String caseid,String courtid){
		this.caseid=caseid;
		this.courtid=courtid;
	}
	//执行
	public void run(){
		while(true){
			try{
				DrCase cases=CaseService.getById(this.caseid);
				if(cases==null){
					continue;
				}
				if(!new BigDecimal(1).equals(cases.getStatus())){
					break;
				}
				log.info("----------------转写开始-------------------------");
				DrLiveTemp lasttemp=CourtLiveService.getLastOne(this.caseid);
				if(lasttemp!=null){
					log.info("最后一个节点："+JSONObject.toJSONString(lasttemp));
				}
				//读取redis
				Jedis jedis= RedisUtilbak.getJedis();
				//取出第一条并删除redis的第一条
				String lastOne=jedis.lpop(this.caseid);
				if(lastOne!=null){
					log.info("读取后："+lastOne);
				}
				DrLiveTemp temp=JSONObject.parseObject(lastOne, DrLiveTemp.class);
				if(temp==null){
					continue;
				}
				if(temp.getBeginTime()==null){
					continue;
				}
				List<DrMicrophone> mics=DrMicroService.getMicroByCourt(this.courtid);
				//设置麦克风角色
				for(DrMicrophone phone :mics){
					if(temp.getRole().longValue()==phone.getMicroid().longValue()){
						temp.setRolename(phone.getStaffrole());
					}
				}
//				DrMicrophone phone=DrMicroService.getById(temp.getRole());
				if(lasttemp==null){
					temp.setCaseid(this.caseid);
					temp.setTempid(Codec.UUID());
					temp.setCreatetime(new Date());
					CourtLiveService.insertTemp(temp);
					log.info("插入后："+lastOne);
				}
				else{
					if(temp.getBeginTime().longValue()==lasttemp.getBeginTime().longValue()){
						lasttemp.setCreatetime(new Date());
						lasttemp.setEndTime(temp.getEndTime());
						lasttemp.setRolename(temp.getRolename());
						lasttemp.setRole(temp.getRole());
						lasttemp.setText(temp.getText());
						lasttemp.setCaseid(this.caseid);
						CourtLiveService.updateTemp(lasttemp);
						log.info("更新后："+lastOne);
					}
					else{
						temp.setTempid(Codec.UUID());
						temp.setCaseid(this.caseid);
						temp.setCreatetime(new Date());
						CourtLiveService.insertTemp(temp);
						log.info("插入后："+lastOne);
					}
				}
				
			}catch(Exception e){
				e.printStackTrace();
				log.info("--------------转写失败--------------------");
			}
			//三秒后继续读写
			try {
				log.info("----------------转写结束-------------------------");
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
